package cc.admin.modules.sys.service.impl;

import cc.admin.common.exception.CcAdminException;
import cc.admin.common.sys.vo.LoginUser;
import cc.admin.modules.sys.entity.SysUserSign;
import cc.admin.modules.sys.mapper.SysUserSignMapper;
import cc.admin.modules.sys.service.ISysUserSignService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * Sys user sign service
 *
 * @author 马雪辰
 * @date 2022.08.30 15:39:58
 * @description description
 */
@Service
@Slf4j
public class SysUserSignServiceImpl extends ServiceImpl<SysUserSignMapper, SysUserSign> implements ISysUserSignService {

    @Autowired
    private SysUserSignMapper sysUserSignMapper;

    @Override
    public String querySignText(String password) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        SysUserSign sysUserSign = this.sysUserSignMapper.selectOne(new QueryWrapper<SysUserSign>().eq("user_id", user.getId()));
        if (sysUserSign != null) {
            if (password.equals(sysUserSign.getPassword())) {
                return sysUserSign.getSignBase64();
            } else {
                CcAdminException.runTimeException("签字密码错误!");
            }
        } else {
            CcAdminException.runTimeException("当前用户还没有收集签字信息!");
        }
        return null;
    }

    @Override
    public Map<String, Object> querySignByUserId() {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Map<String, Object> map = new HashMap<>();
        SysUserSign sysUserSign = this.sysUserSignMapper.selectOne(new QueryWrapper<SysUserSign>().eq("user_id", user.getId()));
        if (sysUserSign != null) {
            map.put("existence", true);
            map.put("id", sysUserSign.getId());
        } else {
            map.put("existence", false);
        }
        return map;
    }

    @Override
    public Object queryAppSignText() {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        SysUserSign sysUserSign = this.sysUserSignMapper.selectOne(new QueryWrapper<SysUserSign>().eq("user_id", user.getId()));
        if (sysUserSign != null) {
            return sysUserSign.getSignBase64();
        } else {
            CcAdminException.runTimeException("当前用户还没有收集签字信息!");
        }
        return null;
    }


	@Override
	public String queryUserSignText() {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		SysUserSign sysUserSign = this.sysUserSignMapper.selectOne(new QueryWrapper<SysUserSign>().eq("user_id", user.getId()));
		if (sysUserSign != null) {
			return sysUserSign.getSignBase64();
		}
		return "";
	}
}
